﻿using UnityEngine;

namespace CGF
{
    public abstract class TurnPage_Method
    {
        protected TurnPage main;
        internal Vector2 dragPos, leftPoint, rightPoint;
        public enum AnimState
        {
            Drag,
            Resume,
            TurnOver
        }
        public enum Side
        {
            Left,
            Right,
            Top,
            Bottom
        }
        public AnimState currAnimState = AnimState.Drag;
        public Side currSide = Side.Left;
        protected float turnOverSpeed = 0.1f;

        public TurnPage_Method(TurnPage turnPage, Side side)
        {
            this.main = turnPage;
            currSide = side;
        }

        public abstract void SetTargetsPivot(Vector2 touchPos);

        public abstract void Update();

        /// <summary>
        /// 获取两点连线的中点
        /// </summary>
        protected Vector2 GetCenterBetween2Points(Vector2 point1, Vector2 point2)
        {
            return new Vector2((point1.x + point2.x) * 0.5f, (point1.y + point2.y) * 0.5f);
        }

        /// <summary>
        /// 获取二维平面两条直线的交点
        /// </summary>
        protected Vector2 GetCrossPointOf2Line(Line line1, Line line2)
        {
            return GetCrossPointOf2Line(line1.p1, line1.p2, line2.p1, line2.p2);
        }

        /// <summary>
        /// 获取两直线交点
        /// </summary>
        protected Vector2 GetCrossPointOf2Line(Vector2 a1, Vector2 a2, Vector2 b1, Vector2 b2)
        {
            float a = 0, b = 0;
            int state = 0;
            if (a1.x != a2.x)
            {
                a = (a2.y - a1.y) / (a2.x - a1.x);
                state |= 1;
            }
            if (b1.x != b2.x)
            {
                b = (b2.y - b1.y) / (b2.x - b1.x);
                state |= 2;
            }
            switch (state)
            {
                case 0://L1与L2都平行Y轴
                    {
                        if (a1.x == b1.x)
                        {
                            return new Vector2(0, 0);
                        }
                        else
                        {
                            return new Vector2(0, 0);
                        }
                    }
                case 1://L1存在斜率, L2平行Y轴
                    {
                        float x = b1.x;
                        float y = (a1.x - x) * (-a) + a1.y;
                        return new Vector2(x, y);
                    }
                case 2://L1平行Y轴，L2存在斜率
                    {
                        float x = a1.x;
                        float y = (b1.x - x) * (-b) + b1.y;
                        return new Vector2(x, y);
                    }
                case 3://L1，L2都存在斜率
                    {
                        if (a == b)
                        {
                            return new Vector2(0, 0);
                        }
                        float x = (a * a1.x - b * b1.x - a1.y + b1.y) / (a - b);
                        float y = a * x - a * a1.x + a1.y;
                        return new Vector2(x, y);
                    }
            }
            return new Vector2(0, 0);
        }

    }
}